//
//  ConfirmPurchaseViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/13.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "ConfirmPurchaseViewModel.h"
#import "ConfirmGoodsTableViewCell.h"
#import "RequirementsTableViewCell.h"
#import "ConfirmInformationTableViewCell.h"
#import "ConfirmBottomView.h"
#import "ConfirmTopTimeView.h"
#import "PurchareModel.h"

@interface ConfirmPurchaseViewModel ()<UITableViewDelegate,UITableViewDataSource>
{
    ConfirmBottomView * conView;
    //
    PurchareModel * purcharTopModel;
}
@end

@implementation ConfirmPurchaseViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView thePurchareModel:(nonnull PurchareModel *)p_Model{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        purcharTopModel = p_Model;
        tableView.y = tableView.y + kHeightProportion_In_iPhoneTriploid(30);
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(114);
        tableView.backgroundColor = [UIColor whiteColor];
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
        [superView addSubview:tableView];
        //时间
        ConfirmTopTimeView * topTimeView = [[ConfirmTopTimeView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(30)) inSuperView:superView];
        //地下
        conView = [[ConfirmBottomView alloc] initWithFrame:CGRectMake(0, kY_With_View(tableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(84)) inSuperView:superView];
        [[conView.bottomBut rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(comitePurOrder)]) {
                [self.delegate comitePurOrder];
            }
        }];
        
    }
    return self;
}
#pragma mark -- set
-(void)setCon_Type:(PurchaseType)con_Type{
    _con_Type = con_Type;
    if (_con_Type == PurchaseType_Deposit) {
        conView.bottomBut.title = @"支付定金100元";
    }
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger cellNumber;
    if (section == 0) {
        cellNumber = purcharTopModel.goodsArr.count;
    }else{
        cellNumber = 2;
    }
    return cellNumber;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat cellHeight;
    switch (indexPath.section) {
        case 0:{
            cellHeight = kHeightProportion_In_iPhoneTriploid(107);
        }
            break;
        case 1:{
            if (indexPath.row == 0) {
                cellHeight = kHeightProportion_In_iPhoneTriploid(80);
            }else{
                cellHeight = kHeightProportion_In_iPhoneTriploid(170);
            }
        }
            break;
            
        default:
            cellHeight = 0.1;
            break;
    }
    return cellHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell * cell;
    switch (indexPath.section) {
        case 0:{
            ConfirmGoodsTableViewCell * cell_confirm = [ConfirmGoodsTableViewCell cellWithTableView:tableView];
            cell_confirm.cardModel = purcharTopModel.goodsArr[indexPath.row];
            cell = cell_confirm;
        }
            break;
        case 1:{
            if (indexPath.row == 0) {
                RequirementsTableViewCell * cell_require = [RequirementsTableViewCell cellWithTableView:tableView];
                cell_require.p_model = purcharTopModel;
                cell = cell_require;
            }else{
                ConfirmInformationTableViewCell * cell_infor = [ConfirmInformationTableViewCell cellWithTableView:tableView];
                cell_infor.pur_model = purcharTopModel;
                cell = cell_infor;
            }
        }
            break;
            
        default:
            break;
    }
    if (cell == nil) {
        cell = [BaseTableViewCell cellWithTableView:tableView];
    }
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headsrView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
    return headsrView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}



@end
